package com.example.opengldemo.vertex_magical.shader;

import android.content.res.Resources;
import android.opengl.GLES30;
import android.util.Log;

import com.example.opengldemo.util.MatrixState;
import com.example.opengldemo.util.ShaderBuffer;

import java.util.ArrayList;

public class BallRenderer extends BaseRenderer{


    public BallRenderer(Resources resource, String vertex, String fragment) {
        super(resource, vertex, fragment);
    }
    @Override
    protected void onCreate() {
        super.onCreate();
    }
    @Override
    protected void initBuffer() {
        shaderBuffer = new ShaderBuffer() ;
        initVertexData();
    }

    public void initVertexData() {
        ArrayList<Float> alVertix = new ArrayList<Float>();
        ArrayList<Float> alLongLat=new ArrayList<Float>();
        final int angleSpan = 10;
        int r = 1 ;
        float UNIT_SIZE = 1.5f ;
        for (int vAngle = -90; vAngle < 90; vAngle = vAngle + angleSpan)
        {
            for (int hAngle = 0; hAngle <= 360; hAngle = hAngle + angleSpan)
            {
                float x0=(float)(r*UNIT_SIZE*Math.cos(Math.toRadians(vAngle))*Math.cos(Math.toRadians(hAngle)));
                float y0=(float)(r*UNIT_SIZE*Math.cos(Math.toRadians(vAngle))*Math.sin(Math.toRadians(hAngle)));
                float z0=(float)(r*UNIT_SIZE*Math.sin(Math.toRadians(vAngle)));
                float long0=hAngle; float lat0=vAngle;

                float x1=(float)(r*UNIT_SIZE*Math.cos(Math.toRadians(vAngle))*Math.cos(Math.toRadians(hAngle+angleSpan)));
                float y1=(float)(r*UNIT_SIZE*Math.cos(Math.toRadians(vAngle))*Math.sin(Math.toRadians(hAngle+angleSpan)));
                float z1=(float)(r*UNIT_SIZE*Math.sin(Math.toRadians(vAngle)));
                float long1=hAngle+angleSpan; float lat1=vAngle;

                float x2=(float)(r*UNIT_SIZE*Math.cos(Math.toRadians(vAngle+angleSpan))*Math.cos(Math.toRadians(hAngle+angleSpan)));
                float y2=(float)(r*UNIT_SIZE*Math.cos(Math.toRadians(vAngle+angleSpan))*Math.sin(Math.toRadians(hAngle+angleSpan)));
                float z2=(float)(r*UNIT_SIZE*Math.sin(Math.toRadians(vAngle+angleSpan)));
                float long2=hAngle+angleSpan; float lat2=vAngle+angleSpan;

                float x3=(float)(r*UNIT_SIZE*Math.cos(Math.toRadians(vAngle+angleSpan))*Math.cos(Math.toRadians(hAngle)));
                float y3=(float)(r*UNIT_SIZE*Math.cos(Math.toRadians(vAngle+angleSpan))*Math.sin(Math.toRadians(hAngle)));
                float z3=(float)(r*UNIT_SIZE*Math.sin(Math.toRadians(vAngle+angleSpan)));
                float long3=hAngle; float lat3=vAngle+angleSpan;

                alVertix.add(x1);alVertix.add(y1);alVertix.add(z1);
                alVertix.add(x3);alVertix.add(y3);alVertix.add(z3);
                alVertix.add(x0);alVertix.add(y0);alVertix.add(z0);

                alVertix.add(x1);alVertix.add(y1);alVertix.add(z1);
                alVertix.add(x2);alVertix.add(y2);alVertix.add(z2);
                alVertix.add(x3);alVertix.add(y3);alVertix.add(z3);

                alLongLat.add(long1);alLongLat.add(lat1);
                alLongLat.add(long3);alLongLat.add(lat3);
                alLongLat.add(long0);alLongLat.add(lat0);

                alLongLat.add(long1);alLongLat.add(lat1);
                alLongLat.add(long2);alLongLat.add(lat2);
                alLongLat.add(long3);alLongLat.add(lat3);
            }
        }
        int vCount = alVertix.size() / 3;

        float vertices[] = new float[vCount * 3];
        for (int i = 0; i < alVertix.size(); i++) {
            vertices[i] = alVertix.get(i);
        }
        shaderBuffer.setVertexBuffer(vertices);

        float[] longlat=new float[alLongLat.size()];
        for(int i=0;i<alLongLat.size();i++)
        {
            longlat[i]=alLongLat.get(i);
        }

        shaderBuffer.setTexcoorBuffer(longlat);

    }



    @Override
    protected void onClear() {
        super.onClear();
    }

    float ratio  ;
    @Override
    protected void onSizeChanged(int width, int height) {
        super.onSizeChanged(width, height);
        ratio = (float) width / height;
    }

    @Override
    protected void onSetExpandData() {
        super.onSetExpandData();
        MatrixState.setProjectFrustum(-ratio, ratio, -1, 1, 2, 50);
        MatrixState.setCamera(0,0, 8, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

        MatrixState.pushMatrix() ;
        MatrixState.translate(0 , 0 , MatrixState.translate[2] );
        MatrixState.rotate(MatrixState.rotate[0] , 1 , 0 , 0 );
        MatrixState.rotate(MatrixState.rotate[1] , 0 , 1 , 0 );
        shaderHandle.bindMVPMaxtrixHandle() ;
        MatrixState.popMatrix() ;
    }


    @Override
    protected void onDraw() {
        GLES30.glEnable(GLES30.GL_BLEND);
        GLES30.glBlendFunc(GLES30.GL_SRC_ALPHA, GLES30.GL_ONE_MINUS_SRC_ALPHA);
        GLES30.glBlendEquation(GLES30.GL_FUNC_ADD);

        shaderHandle.bindTexCoorsHandle(shaderBuffer.getTexcoorBuffer()) ;
        shaderHandle.bindVertexHandle(shaderBuffer.getVertexBuffer()) ;
        shaderHandle.run(GLES30.GL_TRIANGLES,shaderBuffer.vCount);

        GLES30.glDisable(GLES30.GL_BLEND);
    }
}
